<?php namespace Phpcmf\Controllers;

/**
 * 二次开发时可以修改本文件，不影响升级覆盖
 */

class Daan extends \Phpcmf\Home\Module
{

	public function index() {
		// 获取get参数
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		$catid = (int)\Phpcmf\Service::L('input')->get('catid');
		$pagesize = (int)\Phpcmf\Service::L('input')->get('pagesize'); // 页数量
		$page = (int)\Phpcmf\Service::L('input')->get('page'); // 当前页
		$shuxing = (int)\Phpcmf\Service::L('input')->get('shuxing'); // 属性 0是最新，1是最热，2热评
		if($shuxing == 1){
			$order = 'hits';
		} elseif($shuxing == 2){
			$order = 'comments';
		} else {
			$order = 'id';
		}
		if(!$pagesize){
			$pagesize = 20; // 默认值
		}
		if(!$page){
			$page = 1; // 默认第一页
		}
		if($catid){
			$page_data = $page; // 从第一页开始
			$per_page = $pagesize; // 每页20条
			$offset = ($page_data - 1) * $per_page;
			$daohang_0 = \Phpcmf\Service::M()->db->table('1_wenda_category')->where('id', $catid)->get()->getRowArray();
			// 初始化为空数组
			$childids = [];
			// 处理childids
			/*if ($daohang_0['childids']) {
				$childids = explode(',', $daohang_0['childids']);
			}

			$in_id = [];
			foreach ($childids as $i) {
				$in_id[] = $i;
			}*/
			$shuju_data = \Phpcmf\Service::M()->table('1_wenda_comment')->where('status', 1)->where('cid', $id)->order_by($order, 'desc')->limit($per_page, $offset)->getAll();
		} else {
			$page_data = $page; // 从第一页开始
			$per_page = $pagesize; // 每页20条
			$offset = ($page_data - 1) * $per_page;
			$shuju_data = \Phpcmf\Service::M()->table('1_wenda_comment')->where('status', 1)->where('cid', $id)->order_by($order, 'desc')->limit($per_page, $offset)->getAll();
		}
			$shuju_data_1 = \Phpcmf\Service::M()->table('1_wenda_comment')->where('status', 1)->where('zuijiadaan', '1')->where('cid', $id)->order_by($order, 'desc')->limit($per_page, $offset)->getAll();
            
		
		// 循环 $shuju_data ， 把 $shuju_data['content'] 中的图片路径替换为 域名 + 图片路径
		$list_data = [];
		foreach ($shuju_data as $key => $val) {
			

			// 内容图片列表
			$tupian_data = dr_get_content_img($val['neirong']);
			$tupian_list = [];
			foreach ($tupian_data as $k => $v) {
				// 去除&quot;及前面的内容
                $v = preg_replace('/.*&quot;/', '', $v);
				$tupian_list[] = dr_thumb($v, 250, 250, 0, 'crop');
			}
			// 头像
			if(dr_avatar($val['uid'])){
				$touxiang = dr_avatar($val['uid']);
			} else {
				$touxiang = 1;
			}
			// 答案图 
			$huidatu_list = [];
			foreach ($val['huidatu'] as $k => $v) {
				if ($k <= 8) {
					$huidatu_list[] = dr_thumb($v['file'], 250, 250, 0, 'crop');
				}
			}
			// 名称
			$userweixin = \Phpcmf\Service::M()->db->table('member_oauth')->where('uid', $val['uid'])->get()->getRowArray();
			$user = dr_member_info($val['uid']);
			if($userweixin){
				$mingcheng = $userweixin['nickname'];
			} else if($user['name']) {
				$mingcheng = $user['name'];
			} else {
				$mingcheng = $user['username'];
			}
			$dianzan = \Phpcmf\Service::M()->db->table('1_wenda_dianzan')->where('tieid', $val['id'])->countAllResults();
			$shoucang = \Phpcmf\Service::M()->db->table('1_wenda_shoucang')->where('tieid', $val['id'])->countAllResults();
			
			// 点赞用户头像
			$dianzan_data = \Phpcmf\Service::M()->table('1_wenda_dianzan')->where('tieid', $val['id'])->getAll();
			$dianzan_touxiang = [];
			foreach ($dianzan_data as $k => $v) {
				$dianzan_touxiang[] = dr_avatar($v['uid']);
			}
			
			// 帖子内容列表
			$list_data[] = [
				'id' => $val['id'],
				'cid' => $val['cid'],
				'uid' => $val['uid'],
				'touxiang' => $touxiang,
				'mingcheng' => $mingcheng,
				'title' => $val['title'],
				'tupian' => $tupian_list,
				'huidatu' => $huidatu_list,
				'jianjie' => dr_strcut(dr_clearhtml($val['content']), 100),
				'fabushijian' => dr_date($val['inputtime'], 'Y-m-d H:i:s'),
				'catid' => $val['catid'],
				'dianzan' => $dianzan,
				'pinglun' => $val['comments'],
				'shoucang' => $shoucang,
				'dianzan_touxiang' => $dianzan_touxiang,
				'moshi' => $val['moshi'],
				'shangjin' => $val['shangjin'],
				'zuijiadaan' => $val['zuijiadaan'],
				'in_reply' => $val['in_reply'],
			];
            
			
		}
		$list_data_zuijia_1 = [];
		foreach ($shuju_data_1 as $key => $val) {
		
				// 帖子内容列表
				$list_data_zuijia_1[] = [
					'id' => $val['id'],
					'cid' => $val['cid'],
					'uid' => $val['uid'],
					'touxiang' => $touxiang,
					'mingcheng' => $mingcheng,
					'title' => $val['title'],
					'tupian' => $tupian_list,
					'huidatu' => $huidatu_list,
					'jianjie' => dr_strcut(dr_clearhtml($val['content']), 100),
					'fabushijian' => dr_date($val['inputtime'], 'Y-m-d H:i:s'),
					'catid' => $val['catid'],
					'dianzan' => $dianzan,
					'pinglun' => $val['comments'],
					'shoucang' => $shoucang,
					'dianzan_touxiang' => $dianzan_touxiang,
					'moshi' => $val['moshi'],
					'shangjin' => $val['shangjin'],
					'zuijiadaan' => $val['zuijiadaan'],
					'in_reply' => $val['in_reply'],
				];

		}
		$tongji = $shoucang = \Phpcmf\Service::M()->db->table('1_wenda')->where('fstatus', 1)->countAllResults();

		$data = [
			'list' => $list_data,
			'tongji' => $tongji,
			'list_zuijia' => $list_data_zuijia_1,
		];

		$this->_json('1', '读取成功', $data);
	}
	// 答案详情
	public function daan_show() {
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		if (!$id) {
			$this->_json('0', '参数错误');
		}
		$wenda_data = \Phpcmf\Service::M()->db->table('1_wenda_comment')->where('id', $id)->get()->getRowArray();
		if (!$wenda_data) {
			$this->_json('0', '数据不存在');
		}

		$touxiang = dr_avatar($wenda_data['uid']);

		$user = dr_member_info($wenda_data['uid']);
				$userweixin = \Phpcmf\Service::M()->db->table('member_oauth')->where('uid', $wenda_data['uid'])->get()->getRowArray();
				if($userweixin){
					$mingcheng = $userweixin['nickname'];
				} else if($user['name']) {
					$mingcheng = $user['name'];
				} else {
					$mingcheng = $user['username'];
				}
		$list_data[] = [
			'id' => $wenda_data['id'],
			'uid' => $wenda_data['uid'],
			'mingcheng' => $mingcheng,
			'tubiao' => dr_thumb($wenda_data['thumb'], 240, 320, 0, 'crop'),
			'touxiang' => $touxiang,
			'title' => $wenda_data['title'],
			'jianjie' => dr_strcut(dr_clearhtml($wenda_data['description']), 100),
			'neirong_data' => htmlspecialchars_decode($wenda_data['content']),
			'fabushijian' => dr_date($wenda_data['inputtime'], 'Y-m-d H:i:s'),
			'kaifangfabu' => $wenda_data['kaifangfabu'],
			'fstatus' => $wenda_data['fstatus'],
			'jiage' => $wenda_data['jiage'],
			'tongji_ke' => $wenda_data['kecheng_total'],
			'hits' => $wenda_data['hits'],
			'shangjin' => $wenda_data['shangjin'],
			'zuijiadaan' => $wenda_data['zuijiadaan'],
			'in_reply' => $wenda_data['in_reply'],
		];

		$this->_json('1', '读取成功', $list_data);

	}
		
		


}
